package com.example.week5_recyclerview;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.week5.R;

import java.util.List;

public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
    private List<ImageInfo> imgList;

    private LayoutInflater layoutInflater;
    private Context context;
    private int currentPos;
    private ViewHolder holder = null;
    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView imgThumb;
        public TextView tv_imgFileName;
        public TextView tv_imgFilePath;
        public TextView tv_img_DateModified;

        View itemView;
        public ViewHolder(View itemView){
            super(itemView);
            if(itemView==null){
                throw new IllegalArgumentException("itemView can not be null");
            }
            this.itemView = itemView;
            imgThumb = (ImageView) itemView.findViewById(R.id.img_thumb);
            tv_imgFileName = (TextView)itemView.findViewById(R.id.img_filename);
            tv_img_DateModified = (TextView)itemView.findViewById(R.id.img_datemodified);
            tv_imgFilePath = (TextView)itemView.findViewById(R.id.img_filepath);
        }
    }

    //新建两个内部接口
    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }
    public interface OnItemLongClickListener{
        void onItemLongClick(View view,int position);
    }
    //新建两个私有变量用于保存监听器以及set方法
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }
    public void setmOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener){
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

    public ImageListAdapter(Context context, List<ImageInfo> imgList){
        this.imgList = imgList;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        ImageInfo imageInfo = imgList.get(position);
        holder.imgThumb.setImageBitmap(BitmapFactory.decodeFile(imgList.get(position).getFile_path()));
        holder.tv_imgFileName.setText(imgList.get(position).getFile_name());
        holder.tv_imgFilePath.setText(imgList.get(position).getFile_path());
        holder.tv_img_DateModified.setText(imgList.get(position).getFile_date());
        //实现回调
        if(mOnItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
        if(mOnItemLongClickListener!=null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener(){
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemLongClickListener.onItemLongClick(holder.itemView,position);
                    return true;
                }
            });
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public String getItem(int position) {
        return imgList.get(position).getFile_name();
    }
    @Override
    public int getItemCount() {
        return imgList.size();
    }

    public void remove(int index){
        imgList.remove(index);
    }

    public void refreshDataSet(){
        notifyDataSetChanged();
    }

}
